package cn.com.pansky.getui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.pansky.util.DateUtil;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.IQueryResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

public class AppPush {

	// 定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
	private static String appId = "mMjIj6cB9C8WpogBdvAtxA";
	private static String appKey = "628nR94P0l5WFIwr59IEg6";
	private static String masterSecret = "lHPmzXkJJlAXgdK7usoF12";
	private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
	private static IGtPush push = null;

	/**
	 * 批量推送
	 * 
	 * @param template
	 * @return
	 * @throws Exception
	 */
	public IPushResult push(TransmissionTemplate template, APNPayload payload)
			throws Exception {
		push = new IGtPush(url, appKey, masterSecret);
		List<String> appIds = new ArrayList<String>();
		appIds.add(appId);
		template.setAppId(appId);
		template.setAppkey(appKey);
		template.setTransmissionType(2);
		// 设置定时展示时间
		template.setDuration(DateUtil.current19Time(),
				DateUtil.getTimeByMinute(10));

		// 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
		AppMessage message = new AppMessage();
		message.setData(template);
		message.setAppIdList(appIds);
		message.setOffline(true);
		message.setOfflineExpireTime(1000 * 600);

		return push.pushMessageToApp(message);
	}

	/**
	 * 单个推送
	 * 
	 * @param template
	 * @param CID
	 * @return
	 * @throws Exception
	 */
	public IPushResult pushtoSingle(TransmissionTemplate template,
			String alias, APNPayload payload) throws Exception {
		push = new IGtPush(url, appKey, masterSecret);
		template.setTransmissionType(2);
		template.setAppId(appId);
		template.setAppkey(appKey);
		// 设置定时展示时间
		template.setDuration(DateUtil.current19Time(),
				DateUtil.getTimeByMinute(10));
		SingleMessage message = new SingleMessage();
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		message.setData(template);
		// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
		message.setPushNetWorkType(0);
		Target target = new Target();
		target.setAlias(alias);
		target.setAppId(appId);

		IPushResult ret = null;
		try {
			ret = push.pushMessageToSingle(message, target);
		} catch (RequestException e) {
			e.printStackTrace();
			ret = push.pushMessageToSingle(message, target, e.getRequestId());
		}
		return ret;
	}

	/**
	 * 获取用户在线状态
	 * @param Alias
	 * @return
	 */
	public static Boolean getUserStatus(String Alias) {
		IGtPush push = new IGtPush(url, appKey, masterSecret);
		IAliasResult queryClient = push.queryClientId(appId, Alias);
		Boolean retVal = false;
		if (queryClient.getClientIdList() != null) {

			if (queryClient.getClientIdList().size() > 0) {
				for (int i = 0; i < queryClient.getClientIdList().size(); i++) {
					IQueryResult abc = push.getClientIdStatus(appId,
							queryClient.getClientIdList().get(i));
					Map<String, Object> map = abc.getResponse();
					if (map.get("result").equals("Online")) {
						retVal = true;
						break;
					}
				}
			}
		}
		return retVal;
	}

	public static void main(String[] args) throws Exception {

		// 定义"点击链接打开通知模板"，并设置标题、内容、链接

		TransmissionTemplate template = new TransmissionTemplate();
		APNPayload payload = new APNPayload();
		try {
			payload.setContentAvailable(0);
			APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
			 alertMsg.setBody("消息内容");
			alertMsg.setLocKey("标题");
			payload.setAlertMsg(alertMsg);
			template.setAPNInfo(payload);
			// 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
			JSONObject pushData = new JSONObject();
			pushData.put("s", "1");// 业务办理类型
			pushData.put("m", "长天测试推送广播");// 通知头
			pushData.put("d", "通知");
			pushData.put("t", "通知");
			
			template.setTransmissionContent(pushData.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		AppPush pusher = new AppPush();
		IPushResult result = pusher.push(template, payload);
		Map<String, Object> str = result.getResponse();
		System.out.println(str);
		System.out.println(pusher.pushtoSingle(template,
				"da278d65-a2e5-48f5-8f40-03ddb5ae457e".replaceAll("-", ""),
				payload).getResponse());
		System.out.println(AppPush
				.getUserStatus("da278d65-a2e5-48f5-8f40-03ddb5ae457e"
						.replaceAll("-", "")));
	}
}
